/*
 * Copyright the original author or authors.
 *
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.mozilla.org/MPL/MPL-1.1.html *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors:
 *   Russell Weir damonsbane/gmail dot com
 */
package hxbase.flash.media;
import hxbase.events.Event;
import hxbase.flash.events.EventTypes;
import hxbase.flash.IFlashWrapper;

class MicrophoneEvents extends EventGroup {
	var onActivity	: Event<Bool->Void>;
	var onStatus : Event<Dynamic->Void>;

	override private function init() {
		onActivity = new Event<Bool->Void>(this);
		onStatus = new Event<Dynamic->Void>(this);
	}
}

class Microphone extends hxbase.flash.events.EventDispatcher<MicrophoneEvents>,
#if flash9
		implements IFlashWrapper<flash.media.Microphone>
#else flash
		implements IFlashWrapper<flash.Microphone>
#end
{
	public static var names(getNames,null) : Array<Dynamic>;
	public var activityLevel(getActivityLevel,null) : Float;
	public var gain(getGain,setGain) : Float;
	public var index(getIndex,null) : Int;
	public var muted(getMuted,null) : Bool;
	public var name(getName,null) : String;
	public var rate(getRate,setRate) : Int; // in AS2 this is Float
	public var silenceLevel(getSilenceLevel,null) : Float; // AS2 is Int
	public var silenceTimeout(getSilenceTimeout,null) : Float; // AS2 is Int
	public var useEchoSuppression(getUseEchoSuppression,null) : Bool;
#if flash9
	public var soundTransform(getSoundTransform,setSoundTransform) : flash.media.SoundTransform;
#else
	public var soundTransform(getSoundTransform,setSoundTransform) : Dynamic;
#end
#if flash9
	var mic : flash.media.Microphone;
#else flash
	var mic : flash.Microphone;
#end

	private function new(?index : Int) {
		super();
		events = new MicrophoneEvents();
#if flash9
		mic = flash.media.Microphone.getMicrophone(index);
#else flash
		mic = flash.Microphone.get(index);
#end
	}

	public function getObject() {
		return mic;
	}

	////////////////////////////////////////////////////
	////		Wrapped statics			////
	////////////////////////////////////////////////////
	// as2 returns Array<String>
	public static function getNames() : Array<Dynamic> {
#if flash9
		return flash.media.Microphone.names;
#else flash
		return flash.Microphone.names;
#end
	}

	// as2 method
	public static function get(?index : Int) : Microphone {
		var m = new Microphone(index);
		return m;
	}

	// as3 method
	public static function getMicrophone(?index : Int) : Microphone {
		var m = new Microphone(index);
		return m;
	}

	////////////////////////////////////////////////////
	////		Wrapped methods			////
	////////////////////////////////////////////////////

	public function setSilenceLevel(silenceLevel : Float, ?timeOut : Int) : Void {
		mic.setSilenceLevel(silenceLevel, timeOut);
	}
	function setUseEchoSuppression(v : Bool) : Void {
		mic.setUseEchoSuppression(useEchoSuppression);
	}

	function setLoopBack(?state : Bool) : Void {
#if flash9
		mic.setLoopBack(state);
#end
	}

	////////////////////////////////////////////////////
	////		Wrapped accessors		////
	////////////////////////////////////////////////////
	public function getActivityLevel() : Float {
		return mic.activityLevel;
	}
	public function getGain() : Float {
		return mic.gain;
	}
	public function setGain(gv:Float) : Float {
		mic.gain = gv;
		return gv;
	}
	public function getIndex() : Int {
		return mic.index;
	}
	public function getMuted() : Bool {
		return mic.muted;
	}
	public function getName() : String {
		return mic.name;
	}
	// AS2 is float
	public function getRate() : Int {
		return mic.rate;
	}
	// AS2 is Float
	public function setRate(r : Int) {
#if flash9
		mic.rate = r;
#else flash
		mic.setRate(r);
#end
		return r;
	}
	// AS2 is Int
	public function getSilenceLevel() : Float {
		return mic.silenceLevel;
	}
	public function getSilenceTimeout() : Float {
		return mic.silenceTimeout;
	}
	public function getUseEchoSuppression() : Bool {
		return mic.useEchoSuppression;
	}
	// f9 only
#if flash9
	public function getSoundTransform() : flash.media.SoundTransform {
		return mic.soundTransform;
#else flash
	/**
		This method only works in flash 9 or higher
	*/
	public function getSoundTransform() : Dynamic {
		return null;
#end
	}


#if flash9
	public function setSoundTransform(s : flash.media.SoundTransform) : flash.media.SoundTransform {
		mic.soundTransform = s;
#else flash
	public function setSoundTransform(s : Dynamic) {
#end
		return s;
	}

	////////////////////////////////////////////////////
	////		Events				////
	////////////////////////////////////////////////////
	public function addEventListener(type :String, listener : Dynamic->Void) {
		switch(type) {
		case EventTypes.ACTIVITY:
			checkSubscribed(mic, type, onActivity);
			events.onActivity.bind(null, listener);
		case EventTypes.STATUS:
			checkSubscribed(mic, type, onStatus);
			events.onStatus.bind(null, listener);
		default:
			trace("["+here.methodName+"] Unknown event "+type);
		}
	}

	function onActivity(e) {
		events.onActivity.call(e);
	}

	function onStatus(e) {
		events.onStatus.call(e);
	}

//function onActivity(active : Bool) : Void
//function onStatus(infoObject : Dynamic) : Void
}
